<!DOCTYPE hrml>
<html>
<head>
    <title>商品分类</title>
    <include file='public/head'>
    <style>
        .layui-width-100{width: 100px !important;}
        .layui-legend-size{font-size:14px !important;}
        .layui-nopadding{padding:0 !important;}
        .layui-nomargin{margin: 0 !important;}
        .layui-bottom{border-bottom:1px solid rgb(226, 226, 226);}
        .layui-input{height:30px !important; }
        .layui-top-margin{margin: 2px 0;}
        .layui-left-margin{margin-left: 10px;}
        .layui-table-page {padding: 7px 0px 0px;}
    </style>
</head>
<body>
    <!-- layui-tab开始 -->
    <div class="layui-tab" lay-fliter="demo-tab">
        <!-- 顶部导航菜单开始 -->
        <div class="layui-tab-nav">
            <ul class="layui-tab-title">
                {:getNav('goods','goods_cats/index')};
            </ul>
        </div>
        <!-- 顶部导航菜单结束 -->
        <br>
        <php>
            $is_admin = \plugins\store\Common::getAllowAccess();
        </php>
        <!-- 内容开始 -->
        <div class="layui-card">
            <!-- 表头按钮开始 -->
            <div class="layui-card-header layui-bg-gray layui-bottom">

                <if condition="$is_admin === true ">
                    <a href="javascript:;" class="layui-btn layui-btn-xs add-btn" ><i class="layui-icon">&#xe654;</i>添加</a>
                    <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-danger" onclick="del_all();"> 批量删除</a>
                    <a href="javascript:;" class="layui-btn layui-btn-xs layui-btn-danger sort-btn" >排序</a>
                    <else />
                    <span>数据列表</span>
                </if>
            </div>
            <!-- 表头按钮结束 -->
            <!-- 数据表格开始 -->
            <form method="post" action="{:cmf_plugin_url('Goods://goods_cats/listorder')}" id="f">
            <table class="layui-table layui-nomargin">
                <thead>
                    <tr>                        
                        <th rowspan="2" style="text-align:center; width: 20px;">
                        <input type="checkbox"  id="check-box-all" >
                        </th>
                        <th>分类ID</th>
                        <th>分类名称</th>
                        <th>分类图标</th>
                        <th>是否推荐</th>
                        <th>是否显示</th>
                        <th>排序号（小->大）</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <volist  name="data" id="vo">
                        <tr lay-fliter="test3">
                            <th style="text-align:center;">
                                <empty name="vo.no" value="true">
                                <input type="checkbox" class="check-box " name="catid[]" value="{$vo.catid}">
                                </empty>
                            </th>
                            <td>{$vo.catid}</td>
                            <td style="text-align: left;"><?php if ($vo['level'] != 0) echo '|'; ?><?php echo str_repeat('-', $vo['level']*5)?>{$vo.catname}</td>
                            <td><if condition="$vo.catimg neq ''"><img src="__ROOT__{$vo.catimg}  " width="50"><else>暂无图片</if>
                            </td>
                            <td>
                                <if condition="$vo.isfloor eq 1">
                                    <span style="color:green">是</span>
                                    <else />
                                    <span style="color:orange">否</span>
                                </if>
                            </td>
                            <td>
                                <if condition="$vo.isshow eq 1">
                                    <span style="color:green">显示</span>
                                    <else />
                                    <span style="color:orange">隐藏</span>
                                </if>
                            </td>
                            <td align="center">
                                <input type="hidden" name="id[]" value="{$vo.catid}" >
                                <input style="width: 50px;text-align:center; "  name="listorder[{$vo.catid}]" type="text" size="5" value="{$vo.catsort |default = 0}">
                            </td>
                            <td width="100">

                                <if condition="$is_admin === true ">
                                    <div class="layui-btn-group">
                                        <button type="button" data-catid="{$vo.catid}" class="layui-btn layui-btn-primary layui-btn-xs updata-btn" title="修改"><i class="layui-icon">&#xe642;</i></button>
                                        <button type="button" data-catid="{$vo.catid}" class="layui-btn layui-btn-primary layui-btn-xs delete-btn" title="删除"><i class="layui-icon">&#xe640;</i></button>
                                    </div>
                                    <else />
                                    <span>无权限</span>
                                </if>
                            </td>
                        </tr>
                    </volist>
                </tbody>
            </table>
            </form>
            <!-- 数据表格结束 -->
        </div>
        <!-- 内容结束 -->
    </div>
    <!-- layui-tab结束 -->
    <include file='public/scripts'>
    <script>
    //全选/取消
    $('#check-box-all').on('click',function(){  
        console.log(1111);
        $('.check-box').prop('checked',$('#check-box-all').prop('checked'));
        console.log($('#check-box-all').prop('checked'));
    }); 


    function sort(){
        $("#f").submit();
    }
    //添加表单
    $('.add-btn').on('click',function(){
        let url = "{:cmf_plugin_url('goods://goods_cats/addpage')}";
        layer.open({
            type: 2//基本层类型  0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
            ,title: '添加'//层的标题
            ,shadeClose: true//是否点击遮罩关闭
            ,shade: 0.8//遮罩
            ,area: ['800px', '500px']//宽高
            ,btn:['提交','关闭']//弹框按钮
            ,fixed:false//固定
            ,offset:'130px'
            ,content: url //iframe的url
            ,yes:function(index, layero){
            //子页面的请求操作都放子页面
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                iframeWin.tijiao();
            }
            ,btn2:function(index, layero){
            }
        });
    });
    //编辑表单
    $('.updata-btn').on('click',function(){
        let catid = $(this).data('catid');
        let url = "{:cmf_plugin_url('goods://goods_cats/editpage')}?catid="+catid;
        layer.open({
            type: 2//基本层类型  0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
            ,title: '修改'//层的标题
            ,shadeClose: true//是否点击遮罩关闭
            ,shade: 0.8//遮罩
            ,area: ['800px', '500px']//宽高
            ,btn:['提交','关闭']//弹框按钮
            ,fixed:false//固定
            ,offset:'130px'
            ,content: url //iframe的url
            ,yes:function(index, layero){
            //子页面的请求操作都放子页面
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                iframeWin.tijiao();
            }
            ,btn2:function(index, layero){
            }
        });
    });


//批量删除
function del_all(){
let text = "确定要删除吗?";
var id = $('input[type=checkbox]:checked').map(function(){return this.value}).get().join(',');
console.log(id);
layer.confirm(text, {icon: 3, title:'提示',offset:'280px', btn: ['删除','取消']}, function(index,layero){
    $.ajax({
        url: "{:cmf_plugin_url('Goods://goods_cats/delete')}",
        data:{catid:id},
        type:'post',
        success:function(data){
            if (data.code == 1) {
                layer.msg(data.msg,{icon:1,time:1500,offset:'280px'},function(){
                    window.location.reload();
                });
            } else {
                layer.msg(data.msg,{icon:2,time:1500,offset:'280px'});
            }
        },
        error:function(data){
            console.log(data);
            layer.msg('删除错误',{time:1200,icon:5,offset:'280px'});
        }
    })
},function(index){
    // alert('取消');
});
}


    //删除
    $('.delete-btn').on('click',function(){
        let f = $(this).data('catid');
        //询问
        layer.confirm('确定要删除吗？',{icon:3,title:'提示',offset:'280px',btn:['删除','取消']}
        ,function(index,layero){
            $.ajax({
                url:"{:cmf_plugin_url('Goods://goods_cats/delete')}",
                type:'post',
                data:{catid:f},
                success:function(data){
                    if (data.code == 1) {
                        layer.msg(data.msg,{icon:1,time:1500,offset:'280px'},function(){
                            window.location.reload();
                        });
                    } else {
                        layer.msg(data.msg,{icon:2,time:1500,offset:'280px'});
                    }
                },
                error:function(data){
                    console.log(data);
                    layer.msg('删除错误',{time:1200,icon:5,offset:'280px'});
                }
            })   
        },function(index){});
    })
    //排序
    $('.sort-btn').on('click',function(){
        var sort = $('#f').serialize();
        $.ajax({
            url:"{:cmf_plugin_url('Goods://goods_cats/listorder')}",
            type:'post',
            data:sort,
            success:function(data){

                if (data.code == 1) {
                    layer.msg(data.msg,{icon:1,time:1200,offset:'280px'},function(){
                        window.location.reload();
                    });
                } else {
                    layer.msg(data.msg,{icon:2,time:1200,offset:'280px'});
                }

            },
            error:function(data){
                console.log(data);
                layer.msg('系统错误',{time:1200,icon:5});
            }
        })
    })
    </script>
</body>
</html>